import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/login/index.dart';
import 'package:flutter_enjoy_plus/pages/notice/index.dart';
import 'package:flutter_enjoy_plus/pages/profile/index.dart';
import 'package:flutter_enjoy_plus/pages/tabs_page.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';

void main() {
  runApp(MaterialApp(
    routes: {
      "/": (context) => const TabsPage(),
      '/detail': (context) => const NoticeDetail(),
      "/login": (context) => const LoginPage(),
      // '/profile': (context) => const ProfilePage(),
    },
    initialRoute: "/", // 默认路由 第一个页面访问哪一个 ability中 loadContent()
    //路由拦截
    onGenerateRoute: (settings) {
      //settings.name 路由名称 settings.arguments 路由参数
      final token = tokenManager.getToken();
      if (token.isEmpty) {
        //token为空
        return MaterialPageRoute(
          builder: (context) => const LoginPage(),
        );
      } else {
        //token存在
        if (settings.name == '/profile') {
          //访问的是个人信息页面
          return MaterialPageRoute(
            builder: (context) => ProfilePage(
              userInfo: settings.arguments as Map<String, dynamic>,
            ),
          );
        }
      }
      return null;
    },
  ));
}
